using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace SkinableFormsLib
{
	//TODO: deal with the focus issues between the ghost form's components and the layered one's
	/// <summary>
	/// This class is using the old trick of showing the controls in a different window resource in order to improve performance drastically;
	/// That method had the disadvantage of setting skippy positions for controls resulting in a general lame effect.
	/// This class deals with that by drawing all the controls from the auxilliary form into this form's _lwBitmap internal variable only when dragging defaulting to the active aux form when not moving.
	/// I suspect users will like this solution more that the heavy LWSkinableForm one, even if this is not so rigurous a that one
	/// </summary>
	public class LWSkinableFormVariant : LWSkinableForm
	{

		#region Variables
		#region Private
		private Form frm;
		#endregion
		#endregion

		#region Constructors
		#region LWSkinableFormVariant() : base()
		public LWSkinableFormVariant()
			: base()
		{

			if (DesignMode)
				return;
			frm = new Form();
			frm.Owner = this;
		}
		#endregion
		#endregion

		#region Methods

		#region Overrides

		#region OnBackgroundImageChanged(EventArgs e)
		protected override void OnBackgroundImageChanged(EventArgs e)
		{
			base.OnBackgroundImageChanged(e);
			UpdateBitmap(null, null);
		}
		#endregion

		#region WorkingThreadMethod()
		internal override void WorkingThreadMethod()
		{
			base.WorkingThreadMethod();
		}
		#endregion

		#region OnControlAdded(ControlEventArgs e)
		protected override void OnControlAdded(ControlEventArgs e)
		{
			base.OnControlAdded(e);
			if (!DesignMode && frm != null)
			{
				if (!(e.Control is ILWControl))
				{
					frm.Controls.Add(e.Control);
				}
			}
		}
		#endregion


		#region OnClosing(System.ComponentModel.CancelEventArgs e)
		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			base.OnClosing(e);
			frm.Close();
		}
		#endregion

		#region OnControlRemoved(ControlEventArgs e)
		protected override void OnControlRemoved(ControlEventArgs e)
		{
			base.OnControlRemoved(e);
			if (!DesignMode && frm != null)
			{
				if (!(e.Control is ILWControl))
				{
					frm.Controls.Remove(e.Control);
				}
			}

		}
		#endregion


		#region OnLoad(EventArgs e)
		protected override void OnLoad(EventArgs e)
		{

			base.OnLoad(e);

			if (DesignMode)
				return;
			frm.FormBorderStyle = FormBorderStyle.None;
			frm.Disposed += new EventHandler(frm_Disposed);
			foreach (Control c in Controls)
			{
				if (!(c is ILWControl))
				{
					frm.Controls.Add(c);
				}
			}
			frm.BackColor = Color.FromArgb(5, 5, 5);
			frm.TransparencyKey = frm.BackColor;
			frm.ShowInTaskbar = false;
			frm.Location = Location;
			frm.Size = Size;
			frm.BringToFront();


			Win32.PostMessageSafe(new System.Runtime.InteropServices.HandleRef(this, this.Handle), Win32.WM_NCLBUTTONDOWN, (IntPtr) Win32.MKS.MK_LBUTTON, IntPtr.Zero);


		}
		#endregion

		#region OnVisibleChanged(EventArgs e)
		protected override void OnVisibleChanged(EventArgs e)
		{
			base.OnVisibleChanged(e);

			if (DesignMode)
				return;
			frm.Visible = Visible;
			if (Visible)
			{
				Win32.PostMessageSafe(new System.Runtime.InteropServices.HandleRef(this, this.Handle), Win32.WM_NCLBUTTONDOWN, (IntPtr) Win32.MKS.MK_LBUTTON, IntPtr.Zero);
			}
		}
		#endregion

		#region WndProc(ref System.Windows.Forms.Message m)
		protected override void WndProc(ref System.Windows.Forms.Message m)
		{
			if (DesignMode)
			{
				base.WndProc(ref m);
				return;
			}
			if (m.Msg == Win32.WM_NCLBUTTONDOWN)
			{
				frm.Hide();
				_lwBitmap = (Bitmap) BackgroundImage;
				foreach (Control c in frm.Controls)
				{
					c.DrawToBitmap(_lwBitmap, new Rectangle(c.Location, c.Size));
				}
				UpdateBitmap(null, null);
			}
			base.WndProc(ref m);
			if (m.Msg == Win32.WM_NCLBUTTONDOWN)
			{
				frm.Show();
				frm.Location = Location;
				frm.Size = Size;
				frm.BringToFront();
			}
		}
		#endregion


		#endregion

		#endregion

		#region Event Handlers
		#region frm_Disposed(object sender, EventArgs e)
		private void frm_Disposed(object sender, EventArgs e)
		{
			if (DesignMode)
				return;
			this.Close();
		}
		#endregion

		#endregion

	}
}
